-
Notifications
You must be signed in to change notification settings - Fork 5.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
expression, parser: fix issue #3691, cast compatibility #3894
Conversation
…scalar function
Merge branch winkyao/fix_issue_3762 into winkyao/cast_compability
…/cast_compability
…s 2, should return datum, not row
…o handle overflow gracely
…/cast_compability
executor/executor_test.go
Outdated
@@ -1097,6 +1099,75 @@ func (s *testSuite) TestBuiltin(c *C) { | |||
result = tk.MustQuery("select cast(-1 as unsigned)") | |||
result.Check(testkit.Rows("18446744073709551615")) | |||
|
|||
// Fix issue #3691, cast compabilities. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/ compabilities/ compatibility
expression/builtin_cast.go
Outdated
@@ -23,12 +23,16 @@ package expression | |||
|
|||
import ( | |||
"strconv" | |||
"strings" | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove this empty line.
expression/builtin_cast.go
Outdated
@@ -23,12 +23,16 @@ package expression | |||
|
|||
import ( | |||
"strconv" | |||
"strings" | |||
|
|||
"math" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move math to line 25
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why make check ignore this?
expression/builtin_cast.go
Outdated
@@ -542,22 +546,24 @@ func (b *builtinCastDecimalAsIntSig) evalInt(row []types.Datum) (res int64, isNu | |||
if isNull || err != nil { | |||
return res, isNull, errors.Trace(err) | |||
} | |||
|
|||
// despite of unsigned or signed, Round is needed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Round is needed for both unsigned and signed.
plan/typeinfer_test.go
Outdated
@@ -106,6 +106,7 @@ func (s *testPlanSuite) TestInferType(c *C) { | |||
{"substr(c_binary, c_int)", mysql.TypeVarString, charset.CharsetBin, mysql.BinaryFlag, 20, types.UnspecifiedLength}, | |||
{"uuid()", mysql.TypeVarString, charset.CharsetUTF8, 0, 36, types.UnspecifiedLength}, | |||
{"bit_length(c_char)", mysql.TypeLonglong, charset.CharsetBin, mysql.BinaryFlag, 10, 0}, | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove this line?
sessionctx/variable/session.go
Outdated
@@ -422,6 +423,19 @@ func (sc *StatementContext) HandleTruncate(err error) error { | |||
return err | |||
} | |||
|
|||
// HandleOverflow treat ErrOverflow as warnings or returns the error bases on the StmtCtx.OverflowAsWarning state. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/ treat/ treats
@@ -1131,6 +1131,11 @@ func ProduceDecWithSpecifiedTp(dec *MyDecimal, tp *FieldType, sc *variable.State | |||
} | |||
} | |||
} | |||
|
|||
if terror.ErrorEqual(err, ErrOverflow) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we put this check into HandleOverflow ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@XuHuaiyu no, because the variable.session.go should not import "types" package, because "types" use "variable" package, it will cause circle import problem
util/types/errors.go
Outdated
@@ -37,35 +39,46 @@ var ( | |||
ErrWrongFieldSpec = terror.ClassTypes.New(codeWrongFieldSpec, "Wrong Field Spec") | |||
// ErrBadNumber is return when parsing an invalid binary decimal number. | |||
ErrBadNumber = terror.ClassTypes.New(codeBadNumber, "Bad Number") | |||
// ErrCastSignedOverflow is returned when positive out-of-range integer, and convert to it's negative complement | |||
ErrCastSignedOverflow = terror.ClassTypes.New(codeUnknown, msgCastSignedOverflow) | |||
// ErrCastNegIntToUnsigned is returned when a negative integer cast to unsigned |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/ ErrCastNegIntToUnsigned/ ErrCastNegIntAsUnsigned be better?
s/ cast/ be casted
s/ unsigned/ an unsigned int
add .
at the end.
util/types/errors.go
Outdated
@@ -37,35 +39,46 @@ var ( | |||
ErrWrongFieldSpec = terror.ClassTypes.New(codeWrongFieldSpec, "Wrong Field Spec") | |||
// ErrBadNumber is return when parsing an invalid binary decimal number. | |||
ErrBadNumber = terror.ClassTypes.New(codeBadNumber, "Bad Number") | |||
// ErrCastSignedOverflow is returned when positive out-of-range integer, and convert to it's negative complement |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- s/ ErrCastSignedOverflow/ ErrCastAsSignedOverflow be better?
- add
.
at the end
executor/prepared.go
Outdated
// see https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sql-mode-strict | ||
// said "For statements such as SELECT that do not change data, invalid values | ||
// generate a warning in strict mode, not an error." | ||
// and and https://dev.mysql.com/doc/refman/5.7/en/out-of-range-and-overflow.html |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and and ?
expression/builtin_cast.go
Outdated
@@ -23,12 +23,16 @@ package expression | |||
|
|||
import ( | |||
"strconv" | |||
"strings" | |||
|
|||
"math" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why make check ignore this?
expression/builtin_cast.go
Outdated
uintRes, err = types.ConvertFloatToUint(sc, floatVal, types.UnsignedUpperBound[mysql.TypeLonglong], mysql.TypeDouble) | ||
res = int64(uintRes) | ||
var ures uint64 | ||
ures, err = to.ToUint() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The old name uintRes
may be more readable.
util/types/errors.go
Outdated
msgOverflow = mysql.MySQLErrName[mysql.ErrDataOutOfRange] | ||
msgOverflow = mysql.MySQLErrName[mysql.ErrDataOutOfRange] | ||
msgTruncatedWrongVal = mysql.MySQLErrName[mysql.ErrTruncatedWrongValue] | ||
msgCastSignedOverflow = "Cast to signed converted positive out-of-range integer to it's negative complement" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/ msgCastSignedOverflow/ msgCastAsSignedOverflow
s/ msgCastNegIntToUnsigned/ msgCastNegIntAsUnsigned
expression/builtin_cast.go
Outdated
@@ -636,6 +641,27 @@ type builtinCastStringAsIntSig struct { | |||
baseIntBuiltinFunc | |||
} | |||
|
|||
func (b *builtinCastStringAsIntSig) handleOverflow(origRes int64, origStr string, origErr error, isNegative bool) (res int64, err error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
may add a comment for this.
expression/builtin_cast_test.go
Outdated
sc.OverflowAsWarning = true | ||
|
||
// cast('18446744073709551616' as unsigned); | ||
tp1 := types.NewFieldType(mysql.TypeLonglong) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will this be more readable?
tp1 := &types.FieldType{
Tp:
Flag:
Charset:
....
}
expression/builtin_cast_test.go
Outdated
// create table t1(s1 time); | ||
// insert into t1 values('11:11:11'); | ||
// select cast(s1 as decimal(7, 2)) from t1; | ||
tpDecimal := types.NewFieldType(mysql.TypeNewDecimal) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
@@ -5909,6 +5909,7 @@ IntegerType: | |||
OptInteger: | |||
{} | |||
| "INTEGER" | |||
| "INT" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a test case in parser_test
sessionctx/variable/session.go
Outdated
@@ -422,6 +423,19 @@ func (sc *StatementContext) HandleTruncate(err error) error { | |||
return err | |||
} | |||
|
|||
// HandleOverflow treats ErrOverflow as warnings or returns the error bases on the StmtCtx.OverflowAsWarning state. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/ bases/ based
…/tidb into winkyao/cast_compability
…/cast_compability
LGTM |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
…/cast_compability
…/tidb into winkyao/cast_compability
fix #3691, also fix #645
this pr will fail the jenkins, so should be merged together.